package cn.rtplay.esp32camcar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;

import cn.rtplay.esp32camcar.databinding.ActivityCorrectionBinding;

public class CorrectionActivity extends AppCompatActivity {
    private final String TAG = "CorrectionActivity";
    private final int MAX_CORRECTION_ANGLE = 30;
    private ActivityCorrectionBinding mViewBinding;
    private UserData mUserSetting;
    private ESP32UDPService mESP32UDPService;
    private int mCorrectionAngle;

    // Code to manage Service lifecycle.
    private final ServiceConnection mServiceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder service) {
            mESP32UDPService = ((ESP32UDPService.LocalBinder) service).getService();
            mESP32UDPService.correctionAngleMode(true);
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            mESP32UDPService.correctionAngleMode(false);
            mESP32UDPService = null;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewBinding = ActivityCorrectionBinding.inflate(getLayoutInflater());
        setContentView(mViewBinding.getRoot());

        mUserSetting = new UserData(this);
        setBackground(mUserSetting.getUserSetting(UserData.PREFERENCES_SETTING_UI_COLOR));
        mCorrectionAngle = mUserSetting.getUserSetting(UserData.PREFERENCES_SETTING_CORRECTION_ANGLE);
        Intent esp32ServiceIntent = new Intent(this, ESP32UDPService.class);
        bindService(esp32ServiceIntent, mServiceConnection, BIND_AUTO_CREATE);

        mViewBinding.left.setOnClickListener(view -> {
            if (mCorrectionAngle > -MAX_CORRECTION_ANGLE) {
                mCorrectionAngle--;
                mESP32UDPService.setCorrectionAngle(mCorrectionAngle);
            }
        });
        mViewBinding.right.setOnClickListener(view -> {
            if (mCorrectionAngle < MAX_CORRECTION_ANGLE) {
                mCorrectionAngle++;
                mESP32UDPService.setCorrectionAngle(mCorrectionAngle);
            }
        });
        mViewBinding.exit.setOnClickListener(view -> onBackPressed());
    }

    @Override
    protected void onStop() {
        mESP32UDPService.correctionAngleMode(false);
        mUserSetting.setUserSetting(UserData.PREFERENCES_SETTING_CORRECTION_ANGLE, mCorrectionAngle);
        super.onStop();
    }

    private void setBackground(int colorIndex) {
        switch (colorIndex) {
            case UserData.VALUE_UI_COLOR_GREY:
                mViewBinding.pager.setBackgroundColor(ContextCompat.getColor(this, R.color.grey));
                break;
            case UserData.VALUE_UI_COLOR_DARKSLATEGRAY:
                mViewBinding.pager.setBackgroundColor(ContextCompat.getColor(this, R.color.darkslategray));
                break;
            case UserData.VALUE_UI_COLOR_MIDNIGHTBLUE:
                mViewBinding.pager.setBackgroundColor(ContextCompat.getColor(this, R.color.midnightblue));
                break;
            case UserData.VALUE_UI_COLOR_SLATEBLUE:
                mViewBinding.pager.setBackgroundColor(ContextCompat.getColor(this, R.color.slateblue));
                break;
            case UserData.VALUE_UI_COLOR_SIENNA:
                mViewBinding.pager.setBackgroundColor(ContextCompat.getColor(this, R.color.sienna));
                break;
            case UserData.VALUE_UI_COLOR_BLACK:
            default:
                mViewBinding.pager.setBackgroundColor(ContextCompat.getColor(this, R.color.black));
        }
    }
}